#!/usr/bin/env bash
set -e
function info { echo -e "\e[32m[info] $*\e[39m"; }
function warn  { echo -e "\e[33m[warn] $*\e[39m"; }
function error { echo -e "\e[31m[error] $*\e[39m"; exit 1; }

warn ""
warn "If you want more control over your own system, run"
warn "Home Assistant as a VM or run Home Assistant Core"
warn "via a Docker container."
warn ""

# Check if we are running on a supported OS
BYPASS_OS_CHECK=${BYPASS_OS_CHECK:-false}
supported_os_id="debian"
supported_os_version_id=("13")

source /etc/os-release
os_supported=false

if [[ "$ID" == "$supported_os_id" ]]; then
    for version in "${supported_os_version_id[@]}"; do
        if [[ "$VERSION_ID" == "$version" ]]; then
            os_supported=true
            break
        fi
    done
fi

if [[ $os_supported == false ]]; then
    if [[ $BYPASS_OS_CHECK != "true" ]]; then
        error "${PRETTY_NAME} is not supported!"
    fi
    warn "Bypassing OS check..."
    warn "${PRETTY_NAME} is not supported!"
    warn "Please DO NOT report issues regarding this OS!"
fi

# Check if we are running on a supported architecture
ARCH=$(uname -m)
if [[ ! "x86_64|aarch64" == *"$ARCH"* ]]; then
    error "${ARCH} is not supported!"
fi

# Check if Modem Manager is enabled
if systemctl is-enabled ModemManager.service &> /dev/null; then
    warn "ModemManager service is enabled. This might cause issue when using serial devices."
fi

# Check dmesg access
if [[ "$(sysctl --values kernel.dmesg_restrict)" != "0" ]]; then
    info "Fix kernel dmesg restriction"
    echo 0 > /proc/sys/kernel/dmesg_restrict
    echo "kernel.dmesg_restrict=0" >> /etc/sysctl.conf
fi

# If the hassio_supervisor service is running or exists, stop it 
if [[ "$(systemctl is-active hassio-supervisor.service)" == "active" ]]; then
    info "Stopping hassio_supervisor service"
    systemctl stop hassio-supervisor.service
fi

# If the hassio_apparmor service is running or exists, stop it
if [[ "$(systemctl is-active hassio-apparmor.service)" == "active" ]]; then
    info "Stopping hassio_apparmor service"
    systemctl stop hassio-apparmor.service
fi

# Check for existing hassio_supervisor container and destroy it
if [[ "$(docker ps -aq -f name=hassio_supervisor)" ]]; then
    # ensure the hassio_supervisor service is stopped
    info "Removing existing hassio_supervisor container"
    docker container rm --force hassio_supervisor > /dev/null
fi

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/NetworkManager/NetworkManager.conf.real /etc/NetworkManager/NetworkManager.conf

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/NetworkManager/system-connections/default.real /etc/NetworkManager/system-connections/default

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/docker/daemon.json.real /etc/docker/daemon.json

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/network/interfaces.real /etc/network/interfaces

dpkg-divert --package homeassistant-supervised --add --rename \
    --divert /etc/systemd/resolved.conf.real /etc/systemd/resolved.conf
